﻿package com.wandian.util
{
	import com.wandian.ApplicationFacade;
	import com.wandian.data.GameData;
	import com.wandian.util.scrollBar.ScrollBarMc;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;

    public class CLZListPageGetPage extends Object
    {
        private var _arrList:Array;
        private var _arrDelete:Array;
        private var _isSelectAll:Boolean = false;
        private var _btnFrist:MovieClip;
        private var _btnEnd:MovieClip;
        private var _btnPrev:MovieClip;
        private var _btnNext:MovieClip;
        private var _btnGo:MovieClip;
        private var _txtInput:TextField;
        private var _txtPaging:TextField;
        private var fun:Function;
        private var _facade:ApplicationFacade;
        private var _arrInfo:Array;
        private var _nPageingThis:int = 1;
        private var _nPageingAll:int = 1;
        private var _nPage:int = 0;
        public static var UPDATE_DATA:String = "UPDATE_DATA";

        /**
         * 分页控件
         * @param arrList
         * @param btnPrev
         * @param btnNext
         * @param txtPaging
         * @param txtInput
         * @param btnGo
         * @param gofun
         * @param btnFrist
         * @param btnEnd
         */
        public function CLZListPageGetPage(arrList:Array, btnPrev:MovieClip, btnNext:MovieClip, txtPaging:TextField = null, txtInput:TextField = null, btnGo:MovieClip = null, gofun:Function = null, btnFrist:MovieClip = null, btnEnd:MovieClip = null) : void
        {
            this._arrDelete = new Array();
            this._facade = ApplicationFacade.getInstance();
            this._arrInfo = new Array();
            this.fun = gofun;
            this._arrList = arrList;
            this._nPage = this._arrList.length;
            this._btnFrist = btnFrist;
            this._btnEnd = btnEnd;
            this._btnPrev = btnPrev;
            this._btnNext = btnNext;
            this._btnGo = btnGo;
            this._txtInput = txtInput;
            this._txtPaging = txtPaging;          
            if (this._txtInput != null&&this._btnGo != null)
            {
                this._btnGo.addEventListener(MouseEvent.CLICK, this.onGoHandler);
            }
            if (this._btnFrist != null)
            {
                this._btnFrist.addEventListener(MouseEvent.CLICK, this.clickLeftHandler);
            }
            if (this._btnEnd != null)
            {
                this._btnEnd.addEventListener(MouseEvent.CLICK, this.clickRightHandler);
            }
            this._btnPrev.addEventListener(MouseEvent.CLICK, this.clickLeftHandler);
            this._btnNext.addEventListener(MouseEvent.CLICK, this.clickRightHandler);
            return;
        }

        private function onGoHandler(event:MouseEvent) : void
        {
            var pageIndex:int = int(this._txtInput.text);
            this.sendNotice(pageIndex);
            return;
        }

        private function sendNotice(pageIndex:int) : void
        {
            this.fun(pageIndex);
            return;
        }

        private function clickRightHandler(event:MouseEvent) : void
        {
            if (this._nPageingThis >= this._nPageingAll)
            {
                return;
            }
			this._nPageingThis++;
            this.sendNotice(this._nPageingThis);
            return;
        }

        private function clickLeftHandler(event:MouseEvent) : void
        {
            if (this._nPageingThis <= 1)
            {
                return;
            }
			this._nPageingThis++;
            this.sendNotice(this._nPageingThis);
            return;
        }

        public function setInfoPage(arrData:Array, pageIndex:int, pageSize:int) : void
        {
            this._nPageingThis = pageIndex;
            this._nPageingAll = pageSize;
            if (this._txtPaging != null)
            {
                this._txtPaging.text = this._nPageingThis.toString() + "/" + this._nPageingAll.toString();
            }
            if (arrData.length == 0)
            {
                this.removeListInfo();
                return;
            }
            this._arrInfo = arrData;
            this.updatePage(arrData);
            return;
        }

        private function updatePage(arrRowData:Array) : void
        {
            var mcRow:MovieClip = null;
            this.removeListInfo();
            if (this._txtPaging != null)
            {
                this._txtPaging.text = this._nPageingThis + "/" + this._nPageingAll;
            }
            var index:int = 0;
            while (index < arrRowData.length)
            {
                
                if (index >= this._arrList.length)
                {
                    break;
                }
                mcRow = this._arrList[index] as MovieClip;
                mcRow.visible = true;
                mcRow.info = arrRowData[index];
                mcRow.id = (this._nPageingThis - 1) * this._nPage + index + 1;
                mcRow.dispatchEvent(new Event(UPDATE_DATA));
                index = index + 1;
            }
            return;
        }

        private function removeListInfo() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this._arrList.length)
            {
                
                this._arrList[_loc_1].visible = false;
                _loc_1 = _loc_1 + 1;
            }
            return;
        }

    }
}
